package com.hrma.controller;

import com.github.pagehelper.Page;
import com.hrma.entity.*;
import com.hrma.services.LeaveApplicationService;
import com.hrma.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/leaveApplication")
@Slf4j
public class LeaveApplicationController {
    private final LeaveApplicationService leaveApplicationService;

    public LeaveApplicationController(LeaveApplicationService leaveApplicationService) {
        this.leaveApplicationService = leaveApplicationService;
    }

    @PostMapping("/add")
    public Result<LeaveApplication> add(@Validated @RequestBody LeaveApplication data){
        log.debug("收到add请求，请求参数：{}", data);
        return Result.ok(leaveApplicationService.add(data));
    }

    @PatchMapping("/update")
    public Result<?> update(@Validated @RequestBody LeaveApplication data){
        log.debug("收到update请求，请求参数：{}", data);
        leaveApplicationService.update(data);
        return Result.ok(null);
    }

    @GetMapping("/select")
    public Result<List<SelectResult>> select() {
        log.debug("收到select请求！");
        return Result.ok(leaveApplicationService.select());

    }

    @PostMapping("/list")
    public Result<ListWithCount<LeaveApplicationVO>> getList(@RequestBody LeaveApplicationDTO data) {
        log.debug("收到list请求，请求参数：{}", data);
        Page<LeaveApplicationVO> res = leaveApplicationService.getList(data);

        return Result.ok(new ListWithCount<>(res.getTotal(), res));
    }

    @PostMapping("/PersonalList")
    public Result<ListWithCount<LeaveApplicationVO>> getPersonalList(@RequestBody LeaveApplicationDTO data) {
        log.debug("收到list请求，请求参数：{}", data);
        Page<LeaveApplicationVO> res = leaveApplicationService.getPersonalList(data);

        return Result.ok(new ListWithCount<>(res.getTotal(), res));
    }
}
